﻿Public Class ThisAddIn

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        'Événement déclencher au lancement de Visio
        VisioPrez.oApp = Application
        ShowHideRibbon()
        'Globals.Ribbons.RibbonPrez.DropDownModele.Items.Clear()
        My.User.InitializeWithWindowsUser()
        AddTemplatesFrom("C:\Program Files\Microsoft Office\Templates\1036")
        AddTemplatesFrom(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Microsoft\Templates")
    End Sub

    Private Sub AddTemplatesFrom(ByVal chemin As String)
        If My.Computer.FileSystem.DirectoryExists(chemin) Then
            Dim oDir As IO.DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(chemin)
            For Each oFileInfo As IO.FileInfo In oDir.GetFiles("*.potx", IO.SearchOption.AllDirectories)
                Dim oItem As Microsoft.Office.Tools.Ribbon.RibbonDropDownItem = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem()
                oItem.Label = oFileInfo.Name.Replace(oFileInfo.Extension, "")
                oItem.Tag = oFileInfo.FullName
                Globals.Ribbons.RibbonPrez.DropDownModele.Items.Add(oItem)
            Next
        End If
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
        'Événement déclenché à la fermeture de visio
    End Sub

    Private Sub Application_DocumentChanged(Doc As Microsoft.Office.Interop.Visio.Document) Handles Application.DocumentChanged

    End Sub

    Private Sub Application_DocumentOpened(Doc As Microsoft.Office.Interop.Visio.Document) Handles Application.DocumentOpened
        'Déclenché à l'ouverture d'un diagramme mais aussi de chacun des modèles vss associé
        If Doc.Name.ToUpper.Contains(".VSD") And Not ListePrez.Contains(Doc.ID) Then
            ShowHideRibbon()
            Dim oPrez As New ClassPrez
            LoadFromVSD(oPrez, Doc)
            oPrez.DocID = Doc.ID
            ListePrez.add(oPrez)
            oPrez.AllerDebut()
            oPrez = Nothing
        End If
    End Sub

    Private Sub Application_BeforeDocumentClose(Doc As Microsoft.Office.Interop.Visio.Document) Handles Application.BeforeDocumentClose
        If ListePrez.Contains(Doc.ID) Then
            ShowHideRibbon()
            Dim oPrez As ClassPrez = ListePrez(Doc.ID)
            ListePrez.Remove(oPrez)
         End If
    End Sub

    Private Sub Application_BeforeDocumentSave(Doc As Microsoft.Office.Interop.Visio.Document) Handles Application.BeforeDocumentSave
        If ListePrez.Contains(Doc.ID) Then
            Dim oPrez As ClassPrez = ListePrez(Doc.ID)
            'Rattraper les ajustements à la diapo suivante qui n'ont pas été pris par un suivant, précédent etc.
            oPrez.ReprendreDiapo()
            SaveInVSD(oPrez, Doc)
            oPrez = Nothing
        End If
    End Sub

    Private Sub Application_BeforeDocumentSaveAs(Doc As Microsoft.Office.Interop.Visio.Document) Handles Application.BeforeDocumentSaveAs
        'Ne déclenche pas un BeforeDocumentSave
        If ListePrez.Contains(Doc.ID) Then
            Dim oPrez As ClassPrez = ListePrez(Doc.ID)
            'Rattraper les ajustements à la diapo suivante qui n'ont pas été pris par un suivant, précédent etc.
            oPrez.ReprendreDiapo()
            SaveInVSD(oPrez, Doc)
            oPrez = Nothing
        End If
    End Sub

    Private Sub Application_DocumentCreated(Doc As Microsoft.Office.Interop.Visio.Document) Handles Application.DocumentCreated
        'Déclenché 1 fois avant Document Opened
        ShowHideRibbon()
        Dim oPrez As New ClassPrez
        oPrez.DocID = Doc.ID
        oPrez.Titre = Doc.Name
        oPrez.UpdateRibbon()
        ListePrez.add(oPrez)
        oPrez.AllerDebut()
        oPrez = Nothing
    End Sub

    Private Sub ShowHideRibbon()
        Dim bVisible As Boolean = Application.ActiveDocument IsNot Nothing
        Globals.Ribbons.RibbonPrez.GroupNavig.Visible = bVisible
        Globals.Ribbons.RibbonPrez.GroupExport.Visible = bVisible
        Globals.Ribbons.RibbonPrez.GroupDiapo.Visible = bVisible
    End Sub

    Private Sub Application_WindowActivated(Window As Microsoft.Office.Interop.Visio.Window) Handles Application.WindowActivated
        'Changement de document
    End Sub

   
End Class
